
#import "MeTableViewController.h"
#import "MeLoginCell.h"
#import "MeSettingCell.h"
#import "LoginViewController.h"
#import "MeDetailTableViewController.h"
#import "MeSettingTableViewController.h"

@interface MeTableViewController ()
/** 存放不同分区行数的数组 */
@property (nonatomic, strong) NSMutableArray *rowCountArr;
/** 存放分区第一行的图片数组 */
@property (nonatomic, strong) NSMutableArray *lineOnetitleIconImageArr;
/** 存放分区第一行的主标题数组 */
@property (nonatomic, strong) NSMutableArray *lineOnetitleArr;

@end

@implementation MeTableViewController

static NSString * const loginID = @"login";
static NSString * const settingID = @"setting";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 标题
    self.navigationItem.title = @"我";
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MeLoginCell class]) bundle:nil] forCellReuseIdentifier:loginID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MeSettingCell class]) bundle:nil] forCellReuseIdentifier:settingID];
    
    // 验证是否登录
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"YES"]) { // 登录
        [self.tableView reloadData];
        [self login];
    } else {
        [self logout];
    }
}

#pragma mark - 登录成功
- (void)login
{
    // 注册注销登录通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:@"logout" object:nil];
    
    // 取出对应的cell
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    MeLoginCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    // 取出头像
    NSString *iconString = [[NSUserDefaults standardUserDefaults] objectForKey:@"icon"];
    [cell.loginIconImageView sd_setImageWithURL:[NSURL URLWithString:iconString]];
    
    // 取出用户名
    cell.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
    
    // 取出总访客量，及今日访客量（目前为假值）
    cell.fansLabel.text = [NSString stringWithFormat:@"总访客:1  今日:0"];
    
    // 移除登录通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"login" object:nil];
}

#pragma mark - 注销登录
- (void)logout
{
    // 注册登录成功通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(login) name:@"login" object:nil];
    
    // 沙盒状态修改
    [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"isLogin"];
    
    // 修改用户名cell
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    MeLoginCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    // 修改头像
    cell.loginIconImageView.image = [UIImage imageNamed:@"topicAvatarDefault0"];
    
    // 修改用户名
    cell.nameLabel.text = @"登录/注册";
    
    // 修改访客量
    cell.fansLabel.text = @"您可以使用QQ、微信或微博账号直接登录";
    
    // 移除注销通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"logout" object:nil];
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.rowCountArr.count;
}

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.rowCountArr[section] integerValue];
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MeLoginCell *cell = [tableView dequeueReusableCellWithIdentifier:loginID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        MeSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:settingID];
        cell.settingIconImageView.image = [UIImage imageNamed:self.lineOnetitleIconImageArr[indexPath.section - 1]];
        cell.settingTitleLabel.text = self.lineOnetitleArr[indexPath.section - 1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    } else {
        return 50;
    }
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) { // 点击了登录
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"YES"]) { // 已登录
            MeDetailTableViewController *detailVC = [[MeDetailTableViewController alloc] init];
            [detailVC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:detailVC animated:YES];
        } else { // 未登录
            [self skipToLoginViewController];
        }
    } else if (indexPath.section == self.lineOnetitleArr.count) { // 点击了设置
        MeSettingTableViewController *meSettingVC = [[MeSettingTableViewController alloc] init];
        meSettingVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:meSettingVC animated:YES];
    } else {
        // 验证是否登录
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"YES"]) { // 登录
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"亲~功能暂未实现" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alert dismissViewControllerAnimated:YES completion:nil];
            });
        } else { // 未登录,跳转登录界面
            [self skipToLoginViewController];
        }
    }
}

#pragma mark - 跳转登录界面
- (void)skipToLoginViewController
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 懒加载
- (NSMutableArray *)rowCountArr
{
    if (!_rowCountArr) {
        _rowCountArr = [@[@"1", @"1", @"1", @"1"] mutableCopy];
    }
    return _rowCountArr;
}

- (NSMutableArray *)lineOnetitleIconImageArr
{
    if (!_lineOnetitleIconImageArr) {
        _lineOnetitleIconImageArr = [@[@"mething", @"mefriends", @"mesetting"] mutableCopy];
    }
    return _lineOnetitleIconImageArr;
}

- (NSMutableArray *)lineOnetitleArr
{
    if (!_lineOnetitleArr) {
        _lineOnetitleArr = [@[@"管理我的糗事", @"我的糗友/群", @"设置"] mutableCopy];
    }
    return _lineOnetitleArr;
}

@end
